package org.yjhking.controller;

import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.yjhking.domain.User;

/**
 * @author YJH
 */
@RestController
// 当前类统一托底方法
@DefaultProperties(defaultFallback = "fallbackMethod")
public class OrderController {
    
    @Autowired
    private RestTemplate restTemplate;
    
    @GetMapping("/getById/{id}")
    @HystrixCommand // 标记方法熔断
    public User getUserById(@PathVariable("id") Long id) {
        // 1.定义要请求的路径，后续不会如此使用
        String url = "http://user-server/getById/" + id;
        // 2.使用RestTemplate工具类发送Http请求调用User接口
        return restTemplate.getForObject(url, User.class);
    }
    
    // 统一降级方法
    public Object fallbackMethod() {
        return new User(-1l, "服务熔断触发", "用户服务不可用！");
    }
}
